(function(angular) {
	'use strict';
	angular.module('fs.waf').factory('sitetplService', sitetplService);
	sitetplService.$inject = [ '$q', '$timeout', 'loggerFactory', 'commonService', 'ajaxServiceFactory', 'modalService', 'gridService', 'arithService' ];
	function sitetplService($q, $timeout, loggerFactory, commonService, ajaxServiceFactory, modalService, gridService, arithService) {
		var log = loggerFactory.getLogger("sitetplService"), sitetplServiceObj = this, ajaxService = ajaxServiceFactory.buildAjaxService({
			getGroupScopeOptions: {
				url : 'do/cda/dataList/globalCompanytype',
				method : 'get'					
			},
		});
		
		
		function initGeneral($scope, ctrl, updateProcessFlag) {
			var defer = $q.defer();
			defer.resolve();
			return defer.promise;
		}
		
		
		function initSitetplRole(initLayoutConfig, $scope, ctrl, updateProcessFlag) {
			//init sitetplRole grid
			ctrl.sitetplRoleInnerGridOptions = gridService.createInnerGridOptions($scope, initLayoutConfig.formConfigs.sitetplRole.tableConfigs.table, sitetplRoleRowSelectionChanged);
			ctrl.sitetplRoleInnerGridOptions.data = 'vm.sitetpl.sitetplRoles';
			ctrl.sitetplRoleInnerGridOptions.verifyData = 'vm.sitetplOld.sitetplRoles';
			function sitetplRoleRowSelectionChanged(eventRow, selectedRows) {
				ctrl.selectedSitetplRoles = selectedRows;
			}
			
			ctrl.addSitetplRole = function(currRow) {
				return $q(function(resolved, rejected){
					// params
					var params = {};
					params.filterRoleNames = [];
					angular.forEach(ctrl.sitetpl.sitetplRoles, function(sitetplRole, index) {
						params.filterRoleNames.push(sitetplRole.roleName);
					});
					modalService.openCustomDialogs({
					   'title': 'Site Role Info',
					   'dialogsModule' : 'cda.sitetplrole.popupedit',
					   'params': params,
					   'btnCallback' : {
						   'ok' : function(sitetplRole) {							
							   angular.merge(currRow, sitetplRole);
							   resolved();
							}
						}		
					});	
				});
			};
			
			ctrl.editSitetplRole = function(currRow) {
				var params = {};
				// for show selected service on popup page
				var _selectedSitetplRole = currRow.entity;
				params.sitetplRole = angular.copy(_selectedSitetplRole);
				params.filterRoleNames = [];
				angular.forEach(ctrl.sitetpl.sitetplRoles, function(sitetplRole, index) {
					if (sitetplRole.roleName != params.sitetplRole.roleName) {
						params.filterRoleNames.push(sitetplRole.roleName);
					}
				});
				modalService.openCustomDialogs({
					   'title': 'Site Role Info',
					   'dialogsModule' : 'cda.sitetplrole.popupedit',
					   'params': params,
					   'btnCallback' : {
							'ok' : function(sitetplRole) {							
								if (!ctrl.sitetpl.sitetplRoles) {								
									ctrl.sitetpl.sitetplRoles = [];
								}
								if (!(sitetplRole.dataStatus == 'C' || sitetplRole.dataStatus == 'U')) {
									if (updateProcessFlag) {
										sitetplRole.dataStatus = 'U';
									} else {
										sitetplRole.dataStatus = 'C';
									}
								}
								angular.extend(_selectedSitetplRole, sitetplRole);
							}
						}		
				});			
			};
			
			ctrl.viewSitetplRole = function(currRow) {		
				var params = {};
				// for show selected service on popup page
				var _selectedSitetplRole = currRow.entity;
				params.sitetplRole = angular.copy(_selectedSitetplRole);
				//
				if (ctrl.sitetplOld && ctrl.sitetplOld.sitetplRoles) {
					var oldModel4SelectedRow;
					angular.forEach(ctrl.sitetplOld.sitetplRoles, function(sitetplRole, index) {
						if (sitetplRole.primaryKey == params.sitetplRole.primaryKey) {
							oldModel4SelectedRow = sitetplRole;
						}
					});
					params.sitetplRoleOld = oldModel4SelectedRow;
				}
				modalService.openCustomDialogs({
				   'title': 'Site Role Info',
				   'dialogsModule' : 'cda.sitetplrole.popupview',
				   'params':params,
				   'btnCallback' : {
						'ok' : function(sitetplRole) {						
						}
					}		
				});
			};
		};
		
		
		function initSiteScopeGroup(initLayoutConfig, $scope, ctrl, updateProcessFlag) {
			//init siteScopeGroup grid
			ctrl.siteScopeGroupInnerGridOptions = gridService.createInnerGridOptions($scope, initLayoutConfig.formConfigs.siteScopeGroup.tableConfigs.table, siteScopeGroupRowSelectionChanged);
			ctrl.siteScopeGroupInnerGridOptions.data = 'vm.sitetpl.siteScopeGroups';
			ctrl.siteScopeGroupInnerGridOptions.verifyData = 'vm.sitetplOld.siteScopeGroups';
			function siteScopeGroupRowSelectionChanged(eventRow, selectedRows) {
				ctrl.selectedSiteScopeGroups = selectedRows;
			}
			
			ctrl.addSiteScopeGroup = function(currRow) {
				return $q(function(resolved, rejected){
					if (!ctrl.sitetpl.sitetplRoles || ctrl.sitetpl.sitetplRoles.length == 0) {
						modalService.openErrorDialogs({
							messages : [ 'js.info.sitetpl.siterole-need-first-add' ]
						});
						return;
					}
					// params
					var params = {};
					params.allSitetplRoles = ctrl.sitetpl.sitetplRoles;
					params.siteScopeGroup = {};
					params.siteScopeGroup.groupScope = 'BANK';
					params.filterGroupNames = [];
					angular.forEach(ctrl.sitetpl.siteScopeGroups, function(siteScopeGroup, index) {
						params.filterGroupNames.push(siteScopeGroup.groupName);
					});
					params.sitetplGroup = params.siteScopeGroup;
					modalService.openCustomDialogs({
					   'title': 'Site Group Info',
					   'dialogsModule' : 'cda.sitetplgroup.popupedit',
					   'params': params,
					   'btnCallback' : {
						   'ok' : function(siteScopeGroup) {							
							   angular.merge(currRow, siteScopeGroup);
							   resolved();
							}
						}		
					});	
				});
			};
			
			ctrl.editSiteScopeGroup = function(currRow) {
				if (!ctrl.sitetpl.sitetplRoles || ctrl.sitetpl.sitetplRoles.length == 0) {
					modalService.openErrorDialogs({
						messages : [ 'js.info.sitetpl.siterole-need-first-add' ]
					});
					return;
				}
				// params
				var params = {};
				params.allSitetplRoles = ctrl.sitetpl.sitetplRoles;
				// for show selected service on popup page
				var _selectedSiteScopeGroup = currRow.entity;
				params.siteScopeGroup = angular.copy(_selectedSiteScopeGroup);
				params.filterGroupNames = [];
				angular.forEach(ctrl.sitetpl.siteScopeGroups, function(siteScopeGroup, index) {
					if (siteScopeGroup.groupName != params.siteScopeGroup.groupName) {
						params.filterGroupNames.push(siteScopeGroup.groupName);
					}
				});
				params.sitetplGroup = params.siteScopeGroup;
				modalService.openCustomDialogs({
					   'title': 'Site Group Info',
					   'dialogsModule' : 'cda.sitetplgroup.popupedit',
					   'params': params,
					   'btnCallback' : {
							'ok' : function(siteScopeGroup) {							
								if (!ctrl.sitetpl.siteScopeGroups) {								
									ctrl.sitetpl.siteScopeGroups = [];
								}
								if (!(siteScopeGroup.dataStatus == 'C' || siteScopeGroup.dataStatus == 'U')) {
									if (updateProcessFlag) {
										siteScopeGroup.dataStatus = 'U';
									} else {
										siteScopeGroup.dataStatus = 'C';
									}
								}
								angular.extend(_selectedSiteScopeGroup, siteScopeGroup);
							}
						}		
				});			
			};
			
			ctrl.viewSiteScopeGroup = function(currRow) {		
				var params = {};
				// for show selected service on popup page
				var _selectedSiteScopeGroup = currRow.entity;
				params.siteScopeGroup = angular.copy(_selectedSiteScopeGroup);
				//
				if (ctrl.sitetplOld && ctrl.sitetplOld.siteScopeGroups) {
					var oldModel4SelectedRow;
					angular.forEach(ctrl.sitetplOld.siteScopeGroups, function(siteScopeGroup, index) {
						if (siteScopeGroup.primaryKey == params.siteScopeGroup.primaryKey) {
							oldModel4SelectedRow = siteScopeGroup;
						}
					});
					params.sitetplGroupOld = oldModel4SelectedRow;
				}
				params.sitetplGroup = params.siteScopeGroup;
				modalService.openCustomDialogs({
				   'title': 'Site Group Info',
				   'dialogsModule' : 'cda.sitetplgroup.popupview',
				   'params':params,
				   'btnCallback' : {
						'ok' : function(siteScopeGroup) {						
						}
					}		
				});
			};
		};
		
		
		function initCompanyScopeGroup(initLayoutConfig, $scope, ctrl, updateProcessFlag) {
			ajaxService.getGroupScopeOptions().then(function(response) {
				ctrl.groupScopeOptions = response.data;
			});
			ctrl.groupScopeChanged = function(value) {
				if (ctrl.sitetplScopeTmp && ctrl.sitetplScopeTmp.groupScope) {
					var selectedScope = ctrl.sitetplScopeTmp.groupScope;
					ctrl.sitetpl.companyScopeGroupsTmp = ctrl.sitetplScopeTmp.groups['companyScopeGroups' + selectedScope] || [];
					$timeout(function() {
						if(ctrl.sitetplOld) ctrl.sitetplOld.companyScopeGroupsTmp = ctrl.sitetplScopeOldTmp.groups['companyScopeGroups' + selectedScope] || [];
					});
				}
			};
			//init companyScopeGroup grid
			ctrl.companyScopeGroupInnerGridOptions = gridService.createInnerGridOptions($scope, initLayoutConfig.formConfigs.companyScopeGroup.tableConfigs.table, companyScopeGroupRowSelectionChanged);
			ctrl.companyScopeGroupInnerGridOptions.data = 'vm.sitetpl.companyScopeGroupsTmp';
			ctrl.companyScopeGroupInnerGridOptions.verifyData = 'vm.sitetplOld.companyScopeGroupsTmp';
			function companyScopeGroupRowSelectionChanged(eventRow, selectedRows) {
				ctrl.selectedCompanyScopeGroups = selectedRows;
			}
			
			ctrl.addCompanyScopeGroup = function() {
				return $q(function(resolved, rejected){
					// params
					if (!ctrl.sitetpl.sitetplRoles || ctrl.sitetpl.sitetplRoles.length == 0) {
						modalService.openErrorDialogs({
							messages : [ 'js.info.sitetpl.siterole-need-first-add' ]
						});
						return;
					}
					if (!ctrl.sitetplScopeTmp.groupScope) {
						modalService.openErrorDialogs({
							messages : [ 'js.info.sitetpl.groupscope-need-first-select' ]
						});
						return;
					}
					// params
					var params = {};
					params.allSitetplRoles = ctrl.sitetpl.sitetplRoles;
					params.companyScopeGroup = {};
					params.companyScopeGroup.groupScope = ctrl.sitetplScopeTmp.groupScope;
					params.filterGroupNames = [];
					angular.forEach(ctrl.sitetpl.companyScopeGroupsTmp, function(companyScopeGroup, index) {
						params.filterGroupNames.push(companyScopeGroup.groupName);
					});
					params.sitetplGroup = params.companyScopeGroup;
					modalService.openCustomDialogs({
					   'title': 'Site Group Info',
					   'dialogsModule' : 'cda.sitetplgroup.popupedit',
					   'params': params,
					   'btnCallback' : {
						   'ok' : function(companyScopeGroup) {
								if (!ctrl.sitetpl.companyScopeGroups) {
									ctrl.sitetpl.companyScopeGroups = [];
								}
								if (!ctrl.sitetplScopeTmp.groups['companyScopeGroups' + ctrl.sitetplScopeTmp.groupScope]) {								
									ctrl.sitetplScopeTmp.groups['companyScopeGroups' + ctrl.sitetplScopeTmp.groupScope] = [];
								}
								companyScopeGroup.dataStatus = 'C';
								ctrl.sitetpl.companyScopeGroups.push(companyScopeGroup);
								ctrl.sitetplScopeTmp.groups['companyScopeGroups' + ctrl.sitetplScopeTmp.groupScope].push(companyScopeGroup);
								//
								if (!ctrl.sitetpl.companyScopeGroupsTmp || ctrl.sitetpl.companyScopeGroupsTmp.length == 0)
									ctrl.sitetpl.companyScopeGroupsTmp = ctrl.sitetplScopeTmp.groups['companyScopeGroups' + ctrl.sitetplScopeTmp.groupScope] || [];
							}
						}		
					});	
					rejected();
				});
			};
			
			ctrl.editCompanyScopeGroup = function(currRow) {
				var params = {};if (!ctrl.sitetpl.sitetplRoles || ctrl.sitetpl.sitetplRoles.length == 0) {
					modalService.openErrorDialogs({
						messages : [ 'js.info.sitetpl.siterole-need-first-add' ]
					});
					return;
				}
				// params
				var params = {};
				params.allSitetplRoles = ctrl.sitetpl.sitetplRoles;
				// for show selected service on popup page
				var _selectedCompanyScopeGroup = currRow.entity;
				params.companyScopeGroup = angular.copy(_selectedCompanyScopeGroup);
				params.filterGroupNames = [];
				angular.forEach(ctrl.sitetpl.companyScopeGroupsTmp, function(companyScopeGroup, index) {
					if (companyScopeGroup.groupName != params.companyScopeGroup.groupName) {
						params.filterGroupNames.push(companyScopeGroup.groupName);
					}
				});
				params.sitetplGroup = params.companyScopeGroup;
				modalService.openCustomDialogs({
					   'title': 'Site Group Info',
					   'dialogsModule' : 'cda.sitetplgroup.popupedit',
					   'params': params,
					   'btnCallback' : {
							'ok' : function(companyScopeGroup) {							
								if (!ctrl.sitetpl.companyScopeGroups) {								
									ctrl.sitetpl.companyScopeGroups = [];
								}
								if (!(companyScopeGroup.dataStatus == 'C' || companyScopeGroup.dataStatus == 'U')) {
									if (updateProcessFlag) {
										companyScopeGroup.dataStatus = 'U';
									} else {
										companyScopeGroup.dataStatus = 'C';
									}
								}
								angular.extend(_selectedCompanyScopeGroup, companyScopeGroup);
							}
						}		
				});			
			};
			
			ctrl.viewCompanyScopeGroup = function(currRow) {		
				var params = {};
				// for show selected service on popup page
				var _selectedCompanyScopeGroup = currRow.entity;
				params.companyScopeGroup = angular.copy(_selectedCompanyScopeGroup);
				//
				if (ctrl.sitetplOld && ctrl.sitetplOld.companyScopeGroups) {
					var oldModel4SelectedRow;
					angular.forEach(ctrl.sitetplOld.companyScopeGroups, function(companyScopeGroup, index) {
						if (companyScopeGroup.primaryKey == params.companyScopeGroup.primaryKey) {
							oldModel4SelectedRow = companyScopeGroup;
						}
					});
					params.sitetplGroupOld = oldModel4SelectedRow;
				}
				params.sitetplGroup = params.companyScopeGroup;
				modalService.openCustomDialogs({
				   'title': 'Site Group Info',
				   'dialogsModule' : 'cda.sitetplgroup.popupview',
				   'params':params,
				   'btnCallback' : {
						'ok' : function(companyScopeGroup) {						
						}
					}		
				});
			};
			
			ctrl.removeCompanyScopeGroup = function(currRow) {
				// delete from ctrl.sitetpl.companyScopeGroups
				angular.forEach(ctrl.selectedCompanyScopeGroups, function(selectedCompanyScopeGroup, key) {
					angular.forEach(ctrl.sitetpl.companyScopeGroups, function(companyScopeGroup, index) {
						if (companyScopeGroup.$$hashKey == selectedCompanyScopeGroup.$$hashKey) {
							if (companyScopeGroup.dataStatus =='C') {
								ctrl.sitetpl.companyScopeGroups.splice(index, 1);
							} else {
								companyScopeGroup.dataStatus = 'D';
							}
						}
					});
				});
				// delete from ctrl.sitetpl.companyScopeGroupsTmp by ui-grid.js, not our self done
			}
		};
		
		
		return {
			initGeneral : initGeneral,
			initSitetplRole : initSitetplRole,
			initSiteScopeGroup : initSiteScopeGroup,
			initCompanyScopeGroup : initCompanyScopeGroup,
		};
	}
})(angular);